package com.mall507.login;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.BaseSP;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.BaseUI;
import com.hzh.frame.util.Util;
import com.hzh.frame.widget.xdialog.XDialog2Button;
import com.mall507.comn.model.User;
import com.mall507.comn.tools.UserTools;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 注册第二步
 */
@Route(path = "/login/RegisterSecond")
public class RegisterSecondUI extends BaseUI {
    EditText nickname;
    EditText password;
    Button button;

    @Override
    protected void onCreateBase() {
        setContentView(R.layout.login_ui_user_reg_step2);
        getTitleView().setContent("注册");
        nickname = findViewById(R.id.nickname);
        password = findViewById(R.id.password);
        button = findViewById(R.id.button);
        initView();
    }

    public void initView() {

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                if (nickname.getText().length() == 0) {
                    alert("取个昵称吧！");
                } else if (password.getText().length() == 0) {
                    alert("请输入密码！");
                } else if (password.getText().length() < 6) {
                    alert("密码需要6位以上才安全!");
                } else {
                    JSONObject params = new JSONObject();
                    try {
                        params.put("nickname", nickname.getText().toString().trim());
                        params.put("phone", getIntent().getStringExtra("phone"));
                        params.put("code", getIntent().getStringExtra("code"));
                        params.put("source", 0);//0为手机号码,1为微信,2为QQ,3为新浪
                        params.put("password", password.getText().toString().trim());
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }
                    BaseHttp.getInstance().write(RegisterSecondUI.this, "login/register", params, new HttpCallBack() {
                        @Override
                        public void onSuccess(JSONObject response) {
                            if (200 == response.optInt("code")) {
                                new XDialog2Button(RegisterSecondUI.this).setMsg("恭喜您注册成功，点击可以直接登录！").setConfirmName("登录", "取消").setCallback(new XDialog2Button.XDialog2ButtonCallBack() {
                                    @Override
                                    public void confirm() {
                                        login();
                                    }

                                    @Override
                                    public void cancel() {
                                    }
                                }).show();
                            } else {
                                alert(response.optString("msg"));
                            }
                        }
                    });
                }
            }
        });
    }

    public void login() {
        User user=new User()
                .setPhone(getIntent().getStringExtra("phone"))
                .setPassword(password.getText().toString().trim())
                .setLoginType("0");
        UserTools.getInstance().login(user, this, new UserTools.LoginCallBack() {
            @Override
            public void showMsg(String msg) {
                alert(msg);
            }

            @Override
            public void loginComplete() {}

            @Override
            public void toRelationPhone(User user) {}

            @Override
            public void userInfoComplete() {
                ARouter.getInstance().build("/main/MainUI").with(getIntent().getExtras()).navigation();
            }
        });
    }
}
